package com.xposed.openfire.smack;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.xposed.openfire.smack.abstracts.AbstractBaseActivity;
import com.xposed.openfire.smack.adapter.UserAddAdapter;
import com.xposed.openfire.smack.adapter.UserListAdapter;
import com.xposed.openfire.smack.entity.FriendAdd;
import com.xposed.openfire.smack.utils.JsonUtils;
import com.xposed.openfire.smack.utils.LogUtils;
import com.xposed.openfire.smack.xmpp.XmppConnection;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.roster.packet.RosterPacket;
import org.jivesoftware.smackx.pubsub.Subscription;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.impl.JidCreate;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AddUserActivity extends AbstractBaseActivity implements UserAddAdapter.OnItemClickListener {

    @BindView(R.id.edit_user_name)
    EditText editUserName;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private UserAddAdapter userAddAdapter;

    private List<FriendAdd> friends = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_friend;
    }

    @Override
    protected void initView() {
        friends.addAll(APPOpenfireSmack.getDaoSession().getFriendAddDao().loadAll());
        userAddAdapter = new UserAddAdapter(this, friends);
        userAddAdapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(userAddAdapter);
    }

    @Override
    public void onEventMainThread(Object object) {
        super.onEventMainThread(object);
    }

    @OnClick({R.id.btn_add_friend})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_add_friend:
                XmppConnection.getInstance().addCreateFriend(editUserName.getText().toString() + "@192.168.1.113", editUserName.getText().toString(), null);
                break;
        }
    }

    @Override
    public void onItemClick(View view, final int position) {
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage("是否添加对方为好友")
                .setPositiveButton("同意并添加", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        XmppConnection.getInstance().subscribed(friends.get(position).getFrom(), Presence.Type.subscribed);
                        XmppConnection.getInstance().addCreateFriend(friends.get(position).getFrom(), null, null);
                        APPOpenfireSmack.getDaoSession().getFriendAddDao().delete(friends.get(position));
                        userAddAdapter.addData(APPOpenfireSmack.getDaoSession().getFriendAddDao().loadAll());

                    }
                })
                .setNeutralButton("拒绝", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).create();
        dialog.show();
    }
}
